package com.sky.controller;

import com.sky.result.Result;
import com.sky.service.VueVisaProductTemplateService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/admin/vueVisaProduct")
@Api(tags = "签证产品模板关联接口")
@Slf4j
public class VueVisaProductTemplateController {

    @Autowired
    private VueVisaProductTemplateService vueVisaProductTemplateService;

    /**
     * 保存产品与模板的关联关系
     *
     * @param params 包含 productId 和 templateIds 的映射
     * @return Result
     */
    @PostMapping("/template")
    @ApiOperation("保存产品模板关联")
    public Result saveProductTemplates(@RequestBody Map<String, Object> params) {
        try {
            Long productId = Long.valueOf(params.get("productId").toString());
            List<Integer> templateIds = (List<Integer>) params.get("templateIds");
            
            vueVisaProductTemplateService.saveProductTemplates(productId, templateIds);
            return Result.success();
        } catch (Exception e) {
            log.error("保存产品模板关联失败：", e);
            return Result.error("保存失败：" + e.getMessage());
        }
    }
    
    @DeleteMapping("/deleteWithTemplates")
    @ApiOperation("删除产品及其关联模板")
    public Result deleteProductWithTemplates(@RequestParam Long productId) {
        try {
            vueVisaProductTemplateService.deleteProductWithTemplates(productId);
            return Result.success();
        } catch (Exception e) {
            log.error("删除产品及其关联模板失败：", e);
            return Result.error("删除失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取产品关联的模板信息
     *
     * @param productId 产品ID
     * @return Result
     */
    @GetMapping("/template/{productId}")
    @ApiOperation("获取产品关联的模板")
    public Result getProductTemplates(@PathVariable Long productId) {
        try {
            List<Map<String, Object>> templates = vueVisaProductTemplateService.getProductTemplates(productId);
            return Result.success(templates);
        } catch (Exception e) {
            log.error("获取产品模板关联失败：", e);
            return Result.error("获取失败：" + e.getMessage());
        }
    }
}